import { createRouter, createWebHistory } from 'vue-router';
import Index from '../pages/Index.vue';
import Categories from '../pages/Categories.vue';
import CategoryProducts from '../pages/CategoryProducts.vue';
import Order from '../pages/Order/Order.vue';
import Cart from '../pages/Cart/Cart.vue';
import Profile from '../pages/Profile.vue';
import Payment from '../pages/Order/Payment.vue';
import Logistics from '../pages/Order/Logistics.vue';
import ProductDetail from '../pages/ProductDetail.vue';
import Login from '../pages/Login.vue';
import Register from '../pages/Register.vue';
import SpecialOffers from '../pages/SpecialOffers.vue';
import AllProducts from '../pages/AllProducts.vue';
import { ElMessage } from 'element-plus';
import { useUserStore } from '../stores/userStore';
const routes = [
  {
    path: '/',
    name: 'Index',
    component: Index,
  },
  {
    path: '/index',
    redirect: '/',
  },
  { path: '/categories', component: Categories },
  { 
    path: '/category/:id', 
    name: 'CategoryProducts',
    component: CategoryProducts,
    props: true 
  },
  { path: '/Order/Order', component: Order, meta: { requiresAuth: true } },
  { 
    path: '/Cart/Cart',
    name: 'Cart',
    component: Cart,
    meta: { requiresAuth: true }
  },
  { 
    path: '/Order/payment',
    name: 'Payment',
    component: Payment,
    meta: { requiresAuth: true }
  },
  { path: '/profile', component: Profile, meta: { requiresAuth: true } },
  { 
    path: '/Order/logistics/:id', 
    name: 'Logistics', 
    component: Logistics,
    props: true,
    meta: { requiresAuth: true }
  },
  {
    path: '/product/:id',
    name: 'ProductDetail',
    component: ProductDetail
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/special-offers',
    name: 'SpecialOffers',
    component: SpecialOffers
  },
  {
    path: '/all-products',
    name: 'AllProducts',
    component: AllProducts
  },
  {
    path: '/admin',
    name: 'Admin',
    component: () => import('../pages/Admin/Admin.vue'),
    redirect: '/admin/statistics',
    children: [
      {
        path: 'products',
        name: 'AdminProducts',
        component: () => import('../pages/Admin/Products.vue'),
        meta: { title: '商品管理' }
      },
      {
        path: 'orders',
        name: 'AdminOrders',
        component: () => import('../pages/Admin/Orders.vue'),
        meta: { title: '订单管理' }
      },
      {
        path: 'inventory',
        name: 'AdminInventory',
        component: () => import('../pages/Admin/Inventory.vue'),
        meta: { title: '库存管理' }
      },
      {
        path: 'statistics',
        name: 'AdminStatistics',
        component: () => import('../pages/Admin/Statistics.vue'),
        meta: { title: '数据统计' }
      },
      {
        path: 'marketing',
        name: 'AdminMarketing',
        component: () => import('../pages/Admin/Marketing.vue'),
        meta: { title: '营销管理' }
      },
      {
        path: 'customers',
        name: 'AdminCustomers',
        component: () => import('../pages/Admin/Customers.vue'),
        meta: { title: '客户管理' }
      },
      {
        path: 'finance',
        name: 'AdminFinance',
        component: () => import('../pages/Admin/Finance.vue'),
        meta: { title: '财务管理' }
      },
      {
        path: 'settings',
        name: 'AdminSettings',
        component: () => import('../pages/Admin/Settings.vue'),
        meta: { title: '店铺设置' }
      }
    ]
  },
  {
    path: '/admin/login',
    name: 'AdminLogin',
    component: () => import('../pages/Admin/Login.vue'),
    meta: { title: '卖家登录' }
  },
];
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});
router.beforeEach((to, from, next) => {
  // 前台用户登录验证
  if (to.meta.requiresAuth) {
    const token = localStorage.getItem('token');
    if (!token) {
      ElMessage.warning('请先登录');
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      });
      return;
    }
  }

  // 管理员路由验证
  if (to.path.startsWith('/admin')) {
    // 管理员登录页面可以直接访问
    if (to.name === 'AdminLogin') {
      next();
      return;
    }
    // 其他管理员页面需要验证登录
    const isLoggedIn = localStorage.getItem('adminToken');
    if (!isLoggedIn) {
      ElMessage.warning('请先登录商家账号');
      next('/admin/login');
      return;
    }
  }

  next();
});
export default router;
